package itsm.isperp.framework.utils;

import itsm.isperp.framework.core.context.ContextHolder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.jdbc.core.JdbcTemplate;

public class HtmlUtil {
  private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符

  /**
   * @param htmlStr
   * @return 删除Html标签
   */
  public static String delHTMLTag(String htmlStr) {
    Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
    Matcher m_script = p_script.matcher(htmlStr);
    htmlStr = m_script.replaceAll(""); // 过滤script标签

    Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
    Matcher m_style = p_style.matcher(htmlStr);
    htmlStr = m_style.replaceAll(""); // 过滤style标签

    Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
    Matcher m_html = p_html.matcher(htmlStr);
    htmlStr = m_html.replaceAll(""); // 过滤html标签

    Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
    Matcher m_space = p_space.matcher(htmlStr);
    htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
    return htmlStr.trim(); // 返回文本字符串
  }

  public static String getTextFromHtml(String htmlStr) {
    htmlStr = delHTMLTag(htmlStr);
    htmlStr = htmlStr.replaceAll("&nbsp;", "");
    htmlStr = htmlStr.substring(0, htmlStr.indexOf("。") + 1);
    return htmlStr;
  }

  public static String Html2Text(String inputString) {
    String htmlStr = inputString; // 含html标签的字符串
    String textStr = "";
    java.util.regex.Pattern p_script;
    java.util.regex.Matcher m_script;
    java.util.regex.Pattern p_style;
    java.util.regex.Matcher m_style;
    java.util.regex.Pattern p_html;
    java.util.regex.Matcher m_html;

    try {
      String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
                                                                                               // }
      String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
                                                                                            // }
      String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

      p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
      m_script = p_script.matcher(htmlStr);
      htmlStr = m_script.replaceAll(""); // 过滤script标签

      p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
      m_style = p_style.matcher(htmlStr);
      htmlStr = m_style.replaceAll(""); // 过滤style标签

      p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
      m_html = p_html.matcher(htmlStr);
      htmlStr = m_html.replaceAll(""); // 过滤html标签

      textStr = htmlStr;
    } catch (Exception e) {
      // Manager.log.debug("neiNewsAction","Html2Text: " + e.getMessage());
    }
    return textStr;// 返回文本字符串
  }

  public static String sendPost(String url, String param) {
    OutputStreamWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
      URL realUrl = new URL(url);
      // 打开和URL之间的连接
      URLConnection conn = realUrl.openConnection();
      // 设置通用的请求属性
      //param = URLEncoder.encode(param, "utf-8");
      //byte[] requestStringBytes = param.getBytes("UTF-8");
      //conn.setRequestProperty("Content-length", "" + requestStringBytes.length);
      conn.setRequestProperty("accept", "*/*");
      conn.setRequestProperty("connection", "Keep-Alive");
      conn.setRequestProperty("user-agent",
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
     // conn.setRequestProperty("Charset", "UTF-8");
      // 发送POST请求必须设置如下两行
      conn.setDoOutput(true);
      conn.setDoInput(true);
      // 获取URLConnection对象对应的输出流
      out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");  
      // 发送请求参数
      out.write(param);
      out.flush();
      // 定义BufferedReader输入流来读取URL的响应
      in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println("发送 POST 请求出现异常！" + e);
      e.printStackTrace();
    }
    // 使用finally块来关闭输出流、输入流
    finally {
      try {
        if (out != null) {
          out.close();
        }
        if (in != null) {
          in.close();
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
    String sql="insert into app_alarm_log(id,post_url,post_param,result,create_date) values(uuid(),'"+url+"','"+param+"','"+result+"',now())";
    jdbcTemplate.execute(sql);
    return result;
  }

}
